import Reflux from "reflux"
import GUtils from "@/utils";
import BaseAction,{addDefaultListerns} from "@/base/BaseAction";

const prefix ="/device/";
const api = {
    search: prefix+"search",
    create: prefix+"create",
    edit: prefix+"edit",
    detail:  prefix+"detail",
    enabled:  prefix+"enabled",

    updateMqtt:  prefix+"updateMqttAddress",
    doDeviceApi: prefix+"doDeviceApi"

};

const createAction = () => {
    const Action = Reflux.createActions({
        ...BaseAction,
        enabled:{asyncResult:true},
        preUpdateMqtt:{},
        updateMqtt:{asyncResult:true},
        doDeviceApi:{asyncResult:true}

    });

    addDefaultListerns(Action, api);

    Action.updateMqtt.listen(function (vo) {
        GUtils.jsonAjax.post(api.updateMqtt, vo).then(this.completed, this.failed);
    });
    Action.doDeviceApi.listen(function (id, op, opValue) {
        op = op.slice(0, 1).toUpperCase() + op.slice(1)
        GUtils.Ajax.post(api.doDeviceApi, {id, op, opValue}).then(this.completed, this.failed);
    });
    return Action;
};

export {api, createAction}